﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Utility;
using Module.BusinessLogic;
using Module.EntitiesInfo;

namespace Web
{
    public partial class dudoan : System.Web.UI.Page
    {
        protected string contentGT;
        protected string contentTL;
        protected string contentCB;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    Getdata();
                }
                GetdataNews();
            }
            catch (Exception)
            {                                
            }
        }
        void GetdataNews()
        {
            DataTable dt = new DataTable();

            dt = CMSContentsBL.GetAll(1, 1443, 5, 1);
            if (dt != null && dt.Rows.Count > 0)
            {
                contentTL = dt.Rows[0]["body"].ToString();
            }

            dt = CMSContentsBL.GetAll(1, 1444, 5, 1);
            if (dt != null && dt.Rows.Count > 0)
            {
                contentGT = dt.Rows[0]["body"].ToString();
            }
            
            dt = CMSContentsBL.GetAll(1, 1445, 5, 1);
            if (dt != null && dt.Rows.Count > 0)
            {
                contentCB = dt.Rows[0]["body"].ToString();
            }   
        }
        void Getdata()
        {
            try
            {
                 List<QuestionInfo> list;
                list = QuestionBL.GetDataQuestion(3);
                if (list != null && list.Count > 0)
                {
                    rptQuestion.DataSource = list;
                    rptQuestion.ItemDataBound += new RepeaterItemEventHandler(rptQuestion_ItemDataBound);
                    rptQuestion.DataBind();
                }
            }
            catch (Exception)
            {                
            }
        }

        void rptQuestion_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            Label lbQuestionId = (Label)e.Item.FindControl("lbQuestionId");           
            RadioButtonList radioAnswer = (RadioButtonList)e.Item.FindControl("radioAnswer");
            List<AnswerInfo> list = AnswerBL.GetAllByQId(Convert.ToInt32(lbQuestionId.Text));
            if (list != null && list.Count > 0)
            {
                radioAnswer.DataSource = list;
                radioAnswer.DataTextField = "Answer";
                radioAnswer.DataValueField = "BitMaskPosition";
                radioAnswer.DataBind();             
            }

        }
     
        protected void tbnxoa_Click(object sender, EventArgs e)
        {
            Response.Redirect("/dudoan.aspx");
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtScore.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập số người trả lời giống bạn ?");
                    txtScore.Focus();
                    return;
                }
                if (txtCode.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập mã bảo vệ ?");
                    txtCode.Focus();
                    return;
                }

                captchaID.ValidateCaptcha(txtCode.Text);
                if (captchaID.UserValidated)
                {                    
                    DataTable dtMember = new DataTable();
                    dtMember = MemberBL.SelectTbl(3, MemberBL.GetValue(), "");
                    int candId = 0;
                    if (dtMember != null && dtMember.Rows.Count > 0)
                    {
                        candId = int.Parse(dtMember.Rows[0]["IdMember"].ToString());// user Idmember khi dang nhap
                        int countYes = 0;
                        QuestionInfo info = new QuestionInfo();
                        foreach (RepeaterItem item in rptQuestion.Items)
                        {
                            Label lbQuestionId = (Label)item.FindControl("lbQuestionId");
                            RadioButtonList radioAnswer = (RadioButtonList)item.FindControl("radioAnswer");
                            info = QuestionBL.GetInfo(Convert.ToInt32(lbQuestionId.Text));
                            if (info != null)
                            {
                                if (radioAnswer.SelectedItem.Selected)
                                {
                                    if (radioAnswer.SelectedValue == info.CorrectBitMask.ToString())
                                    {
                                        countYes = countYes + 1;
                                    }
                                }
                            }
                        }

                        //insert ket qua vao DB

                        CandidateExamInfo candidateExamInfo = new CandidateExamInfo();
                        candidateExamInfo.CandId = candId;
                        candidateExamInfo.ExamId = 0;
                        candidateExamInfo.Status = 1;
                        candidateExamInfo.StartDate = DateTime.Now;
                        candidateExamInfo.EndDate = DateTime.Now;
                        candidateExamInfo.CloseDate = DateTime.Now;
                        candidateExamInfo.Choice1 = 0;
                        candidateExamInfo.Choice2 = 0;
                        candidateExamInfo.Choice3 = 0;
                        candidateExamInfo.Choice4 = 0;
                        candidateExamInfo.TotalCorrect = countYes;
                        candidateExamInfo.Score = Convert.ToInt32(txtScore.Text);
                        int i = CandidateExamBL.Insert(candidateExamInfo);
                        if (i > 0)
                        {
                            MessageBox.Show("Bạn đã gửi thành công");
                        }
                        else
                        {
                            MessageBox.Show("Đã có lỗi xảy ra trong quá trình xử lý");
                        }
                    }
                    
                }
                else
                {
                    txtCode.Focus();
                    MessageBox.Show("Mã bảo vệ không đúng");
                   
                }                
            }
            catch (Exception)
            {
                MessageBox.Show("Đã có lỗi xảy ra trong quá trình xử lý");
            }
        }
    }
}